﻿$(function () {
    var turnBackurl = $("#turnBackurl").val();
    window.history.pushState("", "", turnBackurl);
    $("#SecurityCodeWait").css("color", "#999999");
});
//当输入11位手机号时将获取验证码显示为红色
$(document).ready(function () {
    $("#userName").bind('input propertychange', function () {
        var keyword = $("#userName").val();
        if (keyword.length == 11) {
            $("#SecurityCodeWait").css("color", "#FF571F");
        }
    });
});
//获取注册验证码
function getSecurityCode(obj, userName, type) {
    var registerRequestJson = {
        'uid': userName,
        'type': type
    };
    var command = "get_identifying_code";
    doAjax(registerRequestJson, command, function (msg) {
        if (msg) {
            if (msg.result == "0") {
                //获取成功
            } else if (msg.result == "1") {
                $("#userName").focus();
                venderDialog("服务器忙，请稍后重试！");
                obj.removeAttribute("disabled");
                obj.value = "获取验证码";
                SecurityCodeWait = 60;
            } else {
                $("#userName").focus();
                venderDialog("获取验证码失败！");
                obj.removeAttribute("disabled");
                obj.value = "获取验证码";
                SecurityCodeWait = 0;
            }
        }
    });
}
//获取绑定用验证码验证
function getSecurityCodeVal(obj) {
    var userName = $("#userName").val();
    if (!Val.isMobile(userName)) {
        $("#userName").focus();
        venderDialog("请输入正确的手机号！");
    } else {
        SCodeWait(obj);
        getSecurityCode(obj, userName, 3);
    }
}
//验证码获取等待60s
var SecurityCodeWait = 60;
function SCodeWait(o) {
    if (SecurityCodeWait == 0) {
        o.removeAttribute("disabled");
        o.value = "获取验证码";
        SecurityCodeWait = 60;
    } else {
        o.setAttribute("disabled", true);
        o.value = SecurityCodeWait + "秒后重新获取";
        SecurityCodeWait--;
        setTimeout(function () {
            SCodeWait(o);
        },
        1000)
    }
}
//绑定验证
function bindVal() {
    var userName = $("#userName").val();
    var securityCode = $("#securityCode").val();
    if (!Val.isMobile(userName)) {
        $("#userName").focus();
        venderDialog("请输入正确的手机号！");
    } else if (securityCode.length < 6) {
        venderDialog("请输入验证码！");
    } else {
        var returnUrl = $("#returnUrl").val();
        var turnBackurl = $("#turnBackurl").val();
        //绑定并登录
        UserInfoMgr.weixinUserBind(userName, securityCode, function () {
            //判断登录状态
            if (UserInfoMgr.isLogIn()) {
                if (returnUrl != null && returnUrl != "") {
                    window.location.href = returnUrl;
                } else {
                    window.location.href = "/UserCenter/Index";
                }
            } else {
                window.location.href = spliceRegisterUrl(returnUrl, turnBackurl);
            }
        }, function () {
            venderDialog("请输入正确的验证码！");
        });    
    }
}
